package com.bcx.crm.plat.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * Created by ys on 2017/12/14.
 */
public class ServletUtils {
    private static ApplicationContext applicationContext;
    private static Logger logger = LoggerFactory.getLogger(ServletUtils.class);
    public static boolean uriNeedHandle(String uri){
        String appName=SpringContextHolder.getApplicationContext().getApplicationName();
        if (uri.startsWith(appName)){
            uri=uri.replaceFirst(appName,"");
        }
        if (uri.startsWith("/gmp/sys")){
            return true;
        }else {
            String [] var2="web.needTXUriPrefix".split(",");
            int var3=var2.length;
            for (int var4=0;var4<var3;++var4){
                String prefix=var2[var4];
                if (uri.startsWith(prefix)){
                    return true;
                }
            }
        }
        return false;
    }
    //是否是异步请求
    public static boolean isAsync(HttpServletRequest request){
        return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
    }
    public static HttpServletRequest getCurrentRequest(){
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    }
    public static Locale getCurrentLocale(){
        CookieLocaleResolver resolver = getApplicationContext().getBean(CookieLocaleResolver.class);
        return resolver.resolveLocale(getCurrentRequest());
    }

    private static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        ServletUtils.applicationContext = applicationContext;
    }
}
